#ifndef PROCEDUREITEM_H
#define PROCEDUREITEM_H

#include <QStringList>
#include <QObject>
#include <QRectF>
#include "../MainStation/mainwindow.h"

enum ImageEnhancer {
    IE_None,
    IE_AnkleJointAP,
    IE_AnkleJointLAT,
    IE_ChestAP,
    //IE_ChestLAT,
    //IE_ElbowJointLAT,
    IE_HandAP,
    //IE_Head,
    IE_KneeJointAP,
    IE_KneeJointLAT,
    IE_LSpineAP,
    IE_LSpineLAT,
    IE_PelvisAP,
    //IE_Phantom_1,
    //IE_Phantom_2,
    //IE_TChestAP,
    //IE_TChestLAT,
    //IE_TCSpineAP,
    //IE_TCSpineLAT,
    //IE_TElbowJointLAT,
    //IE_THeadAP,
    //IE_TLSpineAP,
    //IE_TLSpineLAT,

    IE_EnhancerCount,
};

struct S_ImageEnhancerNameMap {
    ImageEnhancer ier;
    const char *name;
    const char *config;
};

extern const S_ImageEnhancerNameMap ImageEnhancerNameMap[];

class ProcedureItem {
public:
    enum BodyPart {
        BP_Gastro,
        BP_Head,
        BP_Neck,
        BP_Chest,
        BP_Lung,
        BP_Shoulder,
        BP_Waist,
        BP_Elbow,
        BP_Pelvis,
        BP_Hand,
        BP_Knee,
        BP_Foot,

        BP_PartCount,
    };

    enum BodyType {
        BT_Normal,
        BT_Infant,
        BT_Slim,
        BT_Overweight,

        BT_TypeCount,
    };

    enum BodyPosition {
        BP_PA,
        BP_AP,
        BP_LAT,

        BP_PosCount,
    };

    QString procId;
    BodyPart part;
    BodyType type;
    BodyPosition position;
    double tubeVoltage;
    double tubeCurrent;
    double exposureTime;
    double center;
    double width;
    quint32 imgRotate;
    quint32 imgHFlip;
    quint32 imgVFlip;
    ImageEnhancer imgEhr;
    QRectF rect;

    explicit ProcedureItem(BodyPart bodyPart = BP_Gastro, BodyType bodyType = BT_Normal, BodyPosition bodyPos = BP_PA) :
        part(bodyPart),
        type(bodyType),
        position(bodyPos)
    {
        init();
    }

    QString bodyPartString() {
        if (part < mainWindow->getStringTables().BodyPartStringTable.size())
            return mainWindow->getStringTables().BodyPartStringTable.at(part);
        else return QObject::tr("Unknown");
    }

    QString bodyTypeString() {
        if (type < mainWindow->getStringTables().BodyTypeStringTable.size())
            return mainWindow->getStringTables().BodyTypeStringTable.at(type);
        else return QObject::tr("Unknown");
    }

    QString bodyPosString() {
        if (position < mainWindow->getStringTables().BodyPositionStringTable.size())
            return mainWindow->getStringTables().BodyPositionStringTable.at(position);
        else return QObject::tr("Unknown");
    }

    friend QDataStream &operator<<(QDataStream &out, const ProcedureItem &param) {
        quint32 bodyPart = param.part;
        quint32 bodyType = param.type;
        quint32 bodyPos = param.position;
        quint32 ier = param.imgEhr;
        out << param.procId << bodyPart << bodyType << bodyPos <<  ier <<
               param.tubeVoltage << param.tubeCurrent << param.exposureTime <<
               param.center << param.width << param.rect <<
               param.imgRotate << param.imgHFlip << param.imgVFlip;
        return out;
    }
    friend QDataStream &operator>>(QDataStream &in, ProcedureItem &param) {
        quint32 bodyPart, bodyType, bodyPos, ier;
        in >> param.procId >> bodyPart >> bodyType >> bodyPos >> ier >>
              param.tubeVoltage >> param.tubeCurrent >> param.exposureTime >>
              param.center >> param.width >> param.rect >>
              param.imgRotate >> param.imgHFlip >> param.imgVFlip;
        param.part = (BodyPart)bodyPart;
        param.type = (BodyType)bodyType;
        param.position = (BodyPosition)bodyPos;
        param.imgEhr = (ImageEnhancer)ier;
        return in;
    }

private:
    void init() {
        procId = bodyPartString();
        if (!bodyTypeString().isEmpty()) procId = QString("%1_%2").arg(procId, bodyTypeString());
        if (!bodyPosString().isEmpty()) procId = QString("%1_%2").arg(procId, bodyPosString());
        tubeVoltage = 70;
        tubeCurrent = 50;
        exposureTime = 100;
        center = 0;
        width = 0;
        imgRotate = 0;
        imgHFlip = 0;
        imgVFlip = 0;
        imgEhr = IE_None;

        qRegisterMetaType<BodyPart>("BodyPart");
    }
};

#endif // PROCEDUREITEM_H
